import { createRouter, createWebHistory } from 'vue-router'

// 导入布局组件
import PublicLayout from '@/components/layout/PublicLayout.vue'

// 导入页面组件
import Home from '@/views/public/Home.vue'
import About from '@/views/public/About.vue'
import Login from '@/views/public/Login.vue'
import Register from '@/views/public/Register.vue'
import BasicRumorQuery from '@/views/public/BasicRumorQuery.vue'
import PublicKnowledge from '@/views/public/PublicKnowledge.vue'

// 导入用户路由
import userRoutes from './modules/user.js'
// 导入管理员路由
import adminRoutes from './modules/admin.js'

// 导入认证守卫
import { authGuard } from './guards/auth.js'

const routes = [
  {
    path: '/',
    component: PublicLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: Home,
        meta: {
          title: '万象鉴真 - 多模态AI反诈信息卫士',
          description: '万象鉴真是一个综合性的信息真实性验证与反诈骗平台'
        }
      },
      {
        path: 'about',
        name: 'About',
        component: About,
        meta: {
          title: '关于我们 - 万象鉴真',
          description: '了解万象鉴真平台的使命、愿景和团队信息'
        }
      },
      {
        path: 'rumor-query',
        name: 'RumorQuery',
        component: BasicRumorQuery,
        meta: {
          title: '谣言检测 - 万象鉴真',
          description: '快速识别网络谣言，守护信息真实'
        }
      },
      {
        path: 'knowledge',
        name: 'Knowledge',
        component: PublicKnowledge,
        meta: {
          title: '知识中心 - 万象鉴真',
          description: '提升反诈意识，掌握防护技能'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录 - 万象鉴真',
      description: '登录万象鉴真平台账户',
      hideLayout: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '注册 - 万象鉴真',
      description: '注册万象鉴真平台账户',
      hideLayout: true
    }
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: () => import('@/views/public/ForgotPassword.vue'),
    meta: {
      title: '忘记密码 - 万象鉴真',
      description: '重置万象鉴真平台账户密码',
      hideLayout: true
    }
  },
  // 用户路由
  ...userRoutes,
  // 管理员路由
  ...adminRoutes,
  // 404 页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/errors/NotFound.vue'),
    meta: {
      title: '页面未找到 - 万象鉴真',
      hideLayout: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的位置，返回到保存的位置
    if (savedPosition) {
      return savedPosition
    }
    // 如果有锚点，滚动到锚点
    if (to.hash) {
      return {
        el: to.hash,
        behavior: 'smooth'
      }
    }
    // 否则滚动到顶部
    return { top: 0 }
  }
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = '万象鉴真 - 多模态AI反诈信息卫士'
  }
  
  // 设置页面描述
  if (to.meta && to.meta.description) {
    const metaDescription = document.querySelector('meta[name="description"]')
    if (metaDescription) {
      metaDescription.setAttribute('content', to.meta.description)
    }
  }
  
  // 认证守卫检查
  authGuard(to, from, next)
})

// 全局后置钩子
router.afterEach((to, from) => {
  // 页面加载完成后的处理
  // 可以在这里添加页面访问统计等功能
  console.log(`路由切换: ${from.path} -> ${to.path}`)
})

export default router 